package com.nus.iss.ad.dbhelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.google.gson.Gson;
import com.nus.iss.ad.model.*;
import com.nus.iss.ad.utils.*;

public class OrderDBHelper {
	
	public int[] getPOIDList(){
		
		 //return new int[]{0,1,2};//Hard Code PO Order Ids
		 
		/*
		 String url=MyConfig.BACKEND_PATH + "/api/getPoIds";
		 JSONArray array=JsonParser.getJSONArrayFromUrl(url);
		 int[] result=new int[array.length()];
		 try {
		 for(int i=0;i<array.length();i++){
			 long object;
			
				object = array.getLong(i);
			
			 result[i]=(int)object;
		 }
		 } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 return result;
		*/
		 
		 //using Gson
		 String url=MyConfig.BACKEND_PATH + "/api/getPoIds";
		 String jElement=JsonParser.getStream(url);
		 Gson gson = new Gson();
        int[] data = gson.fromJson(jElement, int[].class);
        if(data==null){
        	data=new int[0];
        }
        return data;
        
        
	 } 
	
	public void closePO(String poId)
	{
		String url=MyConfig.BACKEND_PATH + "/api/closePurchaseOrder"  ;
	     url=url+"/"+poId;
	     
	     JsonParser.postStream(url, poId);
	}

	public Order getOrderById(String poId){
		 String url=MyConfig.BACKEND_PATH + "/api/getAllOpenPO"  ;

		 int intPoId=0;
		 intPoId=Integer.parseInt(poId);
		 
		 Order data=new Order();
		 OrderDetail[] orderDetails=null;
		 
		 JSONArray jsonOrderArr=JsonParser.getJSONArrayFromUrl(url);
		 JSONObject jsonOrder=new JSONObject();
		 
		 try {
			 
		 for(int j=0;j<jsonOrderArr.length();j++){
			 JSONObject tmp=jsonOrderArr.getJSONObject(j);
			 int orderId=tmp.getInt("poId");
			 if(orderId==intPoId){
				 jsonOrder=tmp;
				 data.setPoId(orderId);
			 }
		 }
		 



			 JSONArray jsonOrderDetailArray=jsonOrder.getJSONArray("purchaseOrderDetails");
			 
			 orderDetails=new OrderDetail[jsonOrderDetailArray.length()];
			 
		 for(int i=0;i<jsonOrderDetailArray.length();i++){
			 JSONObject o=jsonOrderDetailArray.getJSONObject(i);
			 OrderDetail d=new OrderDetail();
			 
				d.setId(o.getInt("id"));
			
			 d.setQuantity(o.getInt("quantity"));
			 d.setInventoryId(o.getString("inventoryId"));
			 
			 JSONObject invObject=o.getJSONObject("inventory");
			 Inventory inv=new Inventory();
			 inv.setCatId(invObject.getInt("catId"));
			 inv.setDescription(invObject.getString("description"));
			 d.setInventory(inv);
			 
			 orderDetails[i]=d;
		 }
		 } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 data.setPurchaseOrderDetails(orderDetails);
		 return data;
		 
		 
	     /*
		 String jsonString=JsonParser.getStream(url);
		  Gson gson = new Gson();
         Order data = gson.fromJson(jsonString, Order.class);
         return data;
         */
         
	}
}
//{"employee":null,"supplier":null,"purchaseOrderDetails":[{"inventory":{"category":null,"supplier1Obj":null,"supplier2Obj":null,"supplier3Obj":null,"unit":null,"inventoryId":"S003","description":"Ruler","catId":1,"stock":700,"reorderlLvl":20,"reorderLimit":20,"location":"1","price":1.0,"supplier1":"BanSup","supplier2":"TopSup","supplier3":"CaneSup","unitId":null},"id":1,"poId":0,"inventoryId":"S003","quantity":100}],"poId":0,"raiseDate":"2014-03-01T00:00:00","closeDate":null,"expectedDate":"2014-05-05T00:00:00","status":"open","supplierId":"BanSup","employeeId":"admin"}

